package com.exonline.backstage.controller.course;

import cn.hutool.core.util.IdUtil;
import com.exonline.commons.base.IBaseServlet;
import com.exonline.backstage.entity.Courses;
import com.exonline.backstage.entity.Direction;
import com.exonline.commons.utils.SqlConditionBuilder;
import com.exonline.commons.utils.TokenUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/courses")
public class CoursesServlet extends HttpServlet implements IBaseServlet {
    ObjectMapper mapper = new ObjectMapper();
    CoursesService service = new CoursesService();
    int pageno = 1;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pagenoStr = req.getParameter("pageno");
        if (pagenoStr != null) {
            pageno = Integer.parseInt(pagenoStr);
        }
        String method = req.getParameter("method");
        try {
            //直接查课程,返回课程列表
            if ("queryall".equals(method)) {
                this.query(req, resp);
            }if("queryallreview".equals(method)){
                this.queryallreview(req, resp);
            }else if ("structurequery".equals(method)) {
                this.structureQuery(req, resp);
            } else if ("queryone".equals(method)) {
                this.queryOne(req, resp);
            } else if ("doreview".equals(method)) {
                this.doReview(req, resp);
            } else if ("add".equals(method)) {
                this.add(req, resp);
            } else if ("edit".equals(method)) {
                this.edit(req, resp);
            } else if ("delete".equals(method)) {
                this.delete(req, resp);
            } else if ("deletereview".equals(method)) {
                this.deletereview(req, resp);
            }
        } catch (Exception e) {
            throw new ServletException("课程操作出现错误,错误信息:" + e.getMessage(), e);
        }
    }

    private void queryallreview(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {

        String condition = SqlConditionBuilder.buildCourseCondition(req);
        List<Courses> courses = service.queryAllReviewHistory(condition, 10, pageno);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", courses);
        mapper.writeValue(resp.getWriter(), map);    }


    private void deletereview(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String id = req.getParameter("cid");
        service.deleteById(id);
    }

    private void doReview(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String aid = TokenUtil.getId(req);
            String aname = TokenUtil.getName(req);
            System.out.println("审核人id:"+aid+"审核人名称:"+aname);
            String status = req.getParameter("status");
            if (status != null) {
                int statusInt = status.equals("1") ? 1 : 2;
                String cid = req.getParameter("id");
                String comment = req.getParameter("comment");
                service.update(statusInt,cid,comment,aid);
                Map<String, Object> map = new HashMap<>();
                map.put("code", 200);
                map.put("msg", "课程审核成功");
                map.put("data", cid);
                mapper.writeValue(resp.getWriter(), map);
            }
        } catch (RuntimeException | SQLException | IOException e) {
            throw new RuntimeException("缺少指定参数,错误信息:" + e.getMessage(), e);
        }

    }

    private void structureQuery(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String condition = req.getParameter("condition");
        List<Direction> directions = service.structurequery(condition, 10, pageno);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "结构化查询成功");
        map.put("data", directions);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String condition = SqlConditionBuilder.buildCourseCondition(req);
        List<Courses> courses = service.queryAll(condition, 10, pageno);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", courses);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        service.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        map.put("data", id);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void queryOne(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        Courses courses = service.queryOne(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", courses);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        String tid = req.getParameter("tid");
        String level = req.getParameter("level");
        String name = req.getParameter("name");
        String introduce = req.getParameter("introduce");
        String price = req.getParameter("price");
        String direction = req.getParameter("direction");
        String subject = req.getParameter("subject");
        service.update(id, tid, level, name, introduce, price, direction, subject);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "修改成功");
        map.put("data", id + ":" + name);
        mapper.writeValue(resp.getWriter(), map);
    }

    @Override
    public void add(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = IdUtil.simpleUUID();
        String tid = req.getParameter("tid");
        String level = req.getParameter("clevel");
        String name = req.getParameter("cname");
        String introduce = req.getParameter("cintroduce");
        String price = req.getParameter("cprice");
        String direction = req.getParameter("cdirection");
        String subject = req.getParameter("csubject");
        service.add(id, tid, level, name, introduce, price, direction, subject, 0, 1);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "添加成功");
        map.put("data", name);
        mapper.writeValue(resp.getWriter(), map);
    }
}
